#include"mystdio.h"
#include<sys/types.h>
#include<sys/stat.h>
#include<fcntl.h>
#include<string.h>
#include<stdlib.h>
#include<unistd.h>

const char *to_string(int flag)
{
  if(flag & NONE_FLUSH) return "None";
  else if(flag & LINE_FLUSH) return "Line";
  else if(flag & FULL_FLUSH) return "Full";
  return "Unkown";
}

void DebugPrint(_myFILE *fp)
{
  printf("outbuffer:%s\n",fp->outbuffer);
  printf("fd:%d,\n",fp->fileno);
  printf("pos:%d\n",fp->pos);
  printf("cap:%d\n",fp->cap);
  printf("flush_mode:%s\n",to_string(fp->flush_mode));
}

_myFILE *myfopen(const char *pathgname,const char *mode)
{
  int flag=0;
  if(strcmp(mode,"r")==0)
  {
    flag|=O_RDONLY;
  }
  else if(strcmp(mode,"w")==0)
  {
    flag|=(O_CREAT|O_WRONLY|O_TRUNC);
  }
  else if(strcmp(mode,"a")==0)
  {
    flag|=(O_CREAT|O_WRONLY|O_APPEND);
  }
  else
  {
    return NULL;
  }

  int fd=0;
  if(flag & O_WRONLY)
  {
    umask(0);
    fd=open(pathgname,flag,0666);
  }
  else
  {
    fd=open(pathgname,flag);
  }
  if(fd<0)
  {
    return NULL;
  }
  _myFILE *fp=(_myFILE*)malloc(sizeof(_myFILE));
  if(fp==NULL)
  {
    return NULL;
  }
  fp->fileno=fd;
  fp->cap=SIZE;
  fp->pos=0;
  fp->flush_mode=LINE_FLUSH;

  return fp;
}

void my_fflush(_myFILE *fp)
{
  if(fp->pos==0)return ;
  write(fp->fileno,fp->outbuffer,fp->pos);
  fp->pos=0;
}

int my_fwrite(_myFILE *fp,const char *s,int size)
{
  //无缓冲区
  //return write(fp->fileno,s,size);
  //含缓冲区
  //1.写入
  memcpy(fp->outbuffer+fp->pos,s,size);
  fp->pos+=size;
  //2.判断，是否要刷新
  if(fp->flush_mode&LINE_FLUSH)
  {
    if(fp->outbuffer[fp->pos-1]=='\n')
    {
      my_fflush(fp);
    }
  }
  else if ((fp->flush_mode&FULL_FLUSH)&&fp->pos==fp->cap)
  {
    my_fflush(fp);
  }
  return size;
}

void my_fclose(_myFILE *fp)
{
  my_fflush(fp);
  close(fp->fileno);
  free(fp);
}
